# coding=utf-8
import time
from PIL import Image, ImageDraw
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

loc = 0
#278X108
imagewidth = 278*2
imageheight = 108*2
startx = 1615
starty = 461

screenshoot_path = '/Users/dhe/Desktop/screenshoot.png'

def start():
    browser = webdriver.Firefox()
    browser.get('https://login.taobao.com/member/login.jhtml')
    elem = browser.find_element_by_id('J_Quick2Static')  # Find the search box
    #elem.send_keys('seleniumhq' + Keys.RETURN)
    elem.click()
    name = browser.find_element_by_id("TPL_username_1")
    password = browser.find_element_by_id("TPL_password_1")
    loginbtn = browser.find_element_by_id("J_SubmitStatic")
    name.send_keys("15901902994")
    password.send_keys("hexuyao1234")
    loginbtn.click()

    # wait captcha render
    time.sleep(1)

    flag = True
    todosomethingelse(browser, 200)
    while flag == True:
        try:
            arrow = browser.find_element_by_class_name("btn_slide")
        except Exception as e:
            browser.quit();
        #browser.save_screenshot(screenshoot_path)
        #loc = calc_location()
        loc = 298*2
        #draw(loc)
        #filter()
        #print str(loc) + " is offset location"
        drag(browser, loc)
        time.sleep(1.5)
        password = browser.find_element_by_id("TPL_password_1")
        password.send_keys("hexuyao1234")
        refresh(browser)
        time.sleep(0.5)



def refresh(driver):
    print "=========>refresh "
    refresh_link = driver.find_element_by_css_selector(".errloading a")
    refresh_link.click()

def todosomethingelse(driver, loc):
    content = driver.find_element_by_id("content")
    ActionChains(driver) \
        .move_by_offset(xoffset=loc/2, yoffset=400).perform()
    time.sleep(0.2)


def drag(driver, loc):
    arrow = driver.find_element_by_class_name("btn_slide")
    #模拟人的行为
    ActionChains(driver).click_and_hold(arrow).perform()
    time.sleep(0.2)
    print str(loc) + ":" + str(loc/2)
    loc = loc/2
    print loc
    ActionChains(driver).move_by_offset(xoffset=loc - 40, yoffset=1).perform()
    time.sleep(0.0001)
    ActionChains(driver).move_by_offset(xoffset=38, yoffset=2).perform()
    #time.sleep(0.01)
    #ActionChains(driver).move_by_offset(xoffset=1, yoffset=-1).perform()
    ActionChains(driver).release(arrow).perform()
    time.sleep(2)

def filter():
    imagea = Image.open(screenshoot_path)
    im2 = Image.new("P", (imagewidth, imageheight), 255)
    for yoffset in range(imageheight):
        row = []
        for xoffset in range(imagewidth):
            pixelValue = imagea.getpixel(((startx + xoffset), (starty + yoffset)))
            xf = str(hex(pixelValue[0])).replace("0x", "")
            if xf.startswith("5") or xf.startswith("4"):
                #print xoffset
                row.append(".")
                im2.putpixel((xoffset, yoffset), 1)
            else:
                row.append(" ")
                im2.putpixel((xoffset, yoffset), 255)

        #print "".join(row)
    im2.save("/Users/dhe/Desktop/filter.png", "png")
def draw(location):
    imagea = Image.open(screenshoot_path)
    print imagea.size
    draw = ImageDraw.Draw(imagea)
    draw.line((startx + location, starty, startx + location, starty + imageheight), fill=(0x00, 0xFF, 0xFF), width=10)
    del draw
    imagea.save("/Users/dhe/Desktop/drwa.png", "png")


start()# coding=utf-8

#get_track(150)


